// AlarmModel.cpp
#include "AlarmModel.h"
#include "alarmmanager.h"
#include <QDateTime>
#include <QUuid>
#include <QTime>

AlarmEventTableModel::AlarmEventTableModel(AlarmManager *alarmManager1, QObject *parent)
    : QAbstractTableModel(parent), alarmManager(alarmManager1) {
}

int AlarmEventTableModel::rowCount(const QModelIndex &parent) const {
    return alarmManager->alarms.count();
}

int AlarmEventTableModel::columnCount(const QModelIndex &parent) const {
    return 2; // 时间 | 消息 | 重复 | 状态
}

QVariant AlarmEventTableModel::data(const QModelIndex &index, int role) const {
    if (!index.isValid()) return QVariant();

    const AlarmEvent &event = alarmManager->alarms[index.row()];
    if(role == Qt::DisplayRole) {
        switch(index.column()) {
        case 0: return event.time.toString("HH:mm");
        case 1: return event.message;
        default: return QVariant();
        }
    }
    return QVariant();
}

QVariant AlarmEventTableModel::headerData(int section, Qt::Orientation orientation, int role) const {
    if (role != Qt::DisplayRole) return QVariant();

    if (orientation == Qt::Horizontal) {
        switch(section) {
        case 0: return "触发时间";
        case 1: return "提醒内容";
        default: return QVariant();
        }
    }
    return QVariant();
}

// AlarmEventTableModel.cpp 修正插入实现
bool AlarmEventTableModel::insertRows(int row, int count,
                                      const QModelIndex &parent) {
    if (row < 0 || row > rowCount())
        return false;

    beginInsertRows(parent, row, row + count - 1);

    for(int i=0; i<count; ++i){
        alarmManager->alarms.insert(row, AlarmEvent{
                                        QUuid::createUuid().toString(),
                                        QTime::currentTime(),
                                        "新提醒",
                                        0,
                                        true
                                    });
    }

    endInsertRows();
    alarmManager->saveAlarms();
    return true;
}

// AlarmEventTableModel.cpp 修正删除实现
bool AlarmEventTableModel::removeRows(int row, int count,
                                      const QModelIndex &parent) {
    if (row < 0 || row + count > rowCount())
        return false;

    beginRemoveRows(parent, row, row + count - 1);

    for(int i=0; i<count; ++i){
        alarmManager->alarms.removeAt(row);
    }

    endRemoveRows();
    alarmManager->saveAlarms();
    return true;
}

// AlarmEventTableModel.cpp 重写setData方法
bool AlarmEventTableModel::setData(const QModelIndex &index,
                                   const QVariant &value,
                                   int role) {
    if (!index.isValid() || role != Qt::EditRole)
        return false;

//    AlarmEvent &event = alarmManager->alarms[index.row()];

//    if(index.column() == 0) { // 时间验证
//        if(value.toTime() < QTime::currentTime()) {
//            return false;
//        }

//    }

//    if(index.column() == 1) { // 消息验证
//        if(value.toString().isEmpty()) {
//            return false;
//        }
//    }

    switch (index.column()) {
    case 0:{
            alarmManager->alarms[index.row()].time = value.toTime();
    }break;
    case 1:{
        alarmManager->alarms[index.row()].message = value.toString();
    }break;

    }
    if(index.column() >=0 && index.column() <=3)
    alarmManager->saveAlarms();

    // 调用基类方法更新数据
    return QAbstractTableModel::setData(index, value, role);
}

AlarmEvent AlarmEventTableModel::getEventAt(int row) const {
    return alarmManager->alarms.at(row);
}

QList<AlarmEvent> AlarmEventTableModel::getAllEvents() const {
    return alarmManager->alarms;
}

// AlarmEventTableModel.cpp 修正实现
Qt::ItemFlags AlarmEventTableModel::flags(const QModelIndex &index) const {
    if (!index.isValid())
        return Qt::NoItemFlags;

    // 默认允许选择和启用
    Qt::ItemFlags defaultFlags = QAbstractTableModel::flags(index);

    // 允许编辑状态列
    if(index.column() >= 0 && index.column() <= 1) {
//    if(index.column() == 3) {
        return defaultFlags | Qt::ItemIsEditable;
    }

    return defaultFlags;
}
